-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local bs = require 'mc.bitstring'

local definition = {}

-- frame_id: the frame identifier of canbus frame, multiple frames in a transmission
-- trans_id: the transmission id of specific canbus message
-- last: indicate that current frame message is last frame of the transmission
-- dest_id : the destination of canbus message transmit to, value 0x3F indicate that the boardcast
-- src_id  : the source of canbus message transmit from, value 0x3F indicate that source is invalid and ignore
-- eff_flag: error message frame flag(0 = data, 1 = error message)[from can.h -- linux]
-- rtr_flag: remote transmission request flag(1 = rtr frame)[from can.h -- linux]
-- extend  : frame format flag(0 = standard 11 bit, 1 = extended 29 bit)[from can.h -- linux]
definition.can_id_pattern = bs.new([[<<
    frame_id:8,
    trans_id:8,
    last:1,
    dest_id:6,
    src_id:6,
    eff_flag:1,
    rtr_flag:1,
    extend:1
>>]])
definition.can_id_offset = 4


definition.sync_property_payload_pattern = bs.new([[<<
    cmd,
    identifier,
    size:2/unit:8,
    checksum:2/unit:8,
    datas/string
>>]])

definition.get_property_payload_pattern = bs.new([[<<
    cmd,
    datas/string
>>]])

-- all commands over canbus
definition.can_cmds = {
    -- debug cmds [0x11, 0x1F], request is odd, response is even
    DEBUG_BOARDCAST_REQ = 0x11,
    DEBUG_BOARDCAST_RSP = 0x12,
    -- heartbeat cmds [0x21, 0x2F], request is odd, response is even
    HEARTBEAT_REQ = 0x21,
    HEARTBEAT_RSP = 0x22,
    -- datasync cmds [0x31, 0x3F], request is odd, response is even
    PROPERTY_SYNC_REQ = 0x31,
    -- remote call cmds [0x41, 0x4F], request is odd, response is even
    REMOTE_CALL_REQ = 0x41,
    REMOTE_CALL_RSP = 0x42,
    REMOTE_GET_PROPERTY_REQ = 0x43,
    REMOTE_GET_PROPERTY_RSP = 0x44
}

-- the protocol id: canbus message received from driver and passed to skynet queue
definition.can_protocol_id = 0xFA

-- the timeout threshold that a message frame transmitted via driver (unit: ms)
definition.can_frame_timeout = {
    SEND = 100,
    RECV = 100
}

-- the timeout threshold that a whole message transmission in monitoring
definition.can_transmission_monitor = {
    INTERVAL = 50, -- unit: ms
    TICKS = 20
}

-- the message dispatch interval in monitoring
definition.can_dispatch = {
    INTERVAL = 100, -- unit: ms
}

-- the status of canbus instances
-- NORMAL: tx and rx of current canbus is in normal state
-- BLOCKED: tx of current canbus is in blocked state, maybe the message is massive or bus is blocked
definition.can_state = {
    NORMAL = 1,
    BLOCKED = 2,
}

-- maximum message total size limit
definition.msg_max_size = {
    CAN = 8 * 1024,
    CANFD = 16 * 1024
}

-- transmit message type
definition.msg_cast_type = {
    UNICAST = 1,
    BOARDCAST = 2
}

-- transmit message destination specified address
definition.dest = {
    BOARDCAST = 0x3F
}

-- transmit message frame type
definition.can_frame_type = {
    CAN = 0,
    CANFD = 1
}

-- transmit message frame maximum size
definition.can_frame_max_size = {
    CAN = 8,
    CANFD = 64
}

-- heartbeat monitor intervals (unit: ms)
definition.heartbeat_interval = {
    SEND = 1000,
    MONITOR = 3000
}

-- heartbeat timeout threshold (unit: times)
definition.heartbeat_threshold = {
    -- record running log while the timeout monitor greater than 3 times
    RUNNING = 3,
    -- record maintenance log while the timeout monitor greater than 5 times
    MAINTENANCE = 5,
    -- report alarm event while the timeout monitor greater than 10 times
    ALARM = 10
}

-- remote property / calling arguments keys
definition.remote_access_keys = {
    SERVICE = "Service:",
    PATH = "Path:",
    INTERFACE = "Interface:",
    METHOD = "Method:",
    SIGNATURE = "Signature:",
    PROPERTIES = "Properties:",
    ARGUMENTS = "Arguments:"
}

-- remote property / calling errors
definition.remote_access_errors = {
    OK = 0,
    INVALID_CHANNEL = 1,
    INVALID_DESTINATION = 2,
    IS_BUSY = 3,
    TIMEOUT = 4
}

-- remote property / calling threshold
definition.remote_access_threshold = {
    CALL = 5000, -- unit: ms
    GET_PROPERTY = 3000, -- unit: ms
    STEP = 100
}

return definition